package com.management.vtribe.controller;

import com.management.vtribe.Bean.Article;
import com.management.vtribe.config.Result;
import com.management.vtribe.config.Tool;
import com.management.vtribe.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class ArticleController {
    @Autowired
    ArticleService articleService;

    @RequestMapping("/getAllArticle")
    public List<Article> getAllArticle(int page, int num){
        return articleService.getAllArticle(page, num);
    }

    @RequestMapping("/getAllArticleCount")
    public int getAllArticleCount(){
        return articleService.getAllArticleCount();
    }

    @RequestMapping("/getArticleByTitle")
    public  List<Article> getArticleByTitle(String title){
        return articleService.getArticleByTitle(title);
    }

    @RequestMapping("/getAllArticleByState")
    public List<Article> getAllArticleByState(String state,int page,int num){
        return articleService.getAllArticleByState(state, page, num);
    }

    @RequestMapping("/getStateArticleCount")
    public int getStateArticleCount(String state){
        return articleService.getStateArticleCount(state);
    }

    @RequestMapping("/getArticleByTS")
    public List<Article> getArticleByTS(String title,String state){
        return articleService.getArticleByTS(title, state);
    }

    @RequestMapping("/updateStateById")
    public Result updateStateById(String id, String state){
        if(id==null||id.equals("")){return new Result(400,"操作失败！");}
        int[] ids = new Tool().toIntArray(id);
        if(state!=null&&(state.equals("发表")||state.equals("草稿")||state.equals("回收"))){
            int res = articleService.updateStateById(ids,state);
            if(res!=0){
                return new Result(200,"操作成功！");
            }else{
                return new Result(400,"操作失败！");
            }
        }else{
            return new Result(400,"state错误！");
        }
    }

    @RequestMapping("/deleteArticleById")
    public Result deleteArticleById(String id){
        if(id==null||id.equals("")){return new Result(400,"删除失败！");}
        int[] ids = new Tool().toIntArray(id);
        int res = articleService.deleteArticleById(ids);
        if(res!=0){
            return new Result(200,"删除成功！");
        }else{
            return new Result(400,"删除失败！");
        }
    }

    @RequestMapping("/updateTCById")
    public Result updateTCById(int id,String title,int cid){
        if(title==null&&title.equals("")){return new Result(400,"标题不能为空！");}
        int res = articleService.updateTCById(id, title, cid);
        if(res==1){
            return new Result(200,"修改成功！");
        }else{
            return new Result(400,"修改失败！");
        }
    }

    @RequestMapping("/updateArticleById")
    public  Result updateArticleById(int id,String content,String title,int cid,String state){
        if(title==null||title.equals("")){return new Result(400,"保存失败！");}
        int res = articleService.updateArticleById(id, content, title, cid,state);
        if(res==1){
            return new Result(200,state+"成功！");
        }else{
            return new Result(400,state+"失败！");
        }
    }

    @RequestMapping("/insertArticle")
    public Result insertArticle(String title,int author,int category,String state,String content){
        int res = articleService.insertArticle(title, author, category, state, content);
        if(res==1){
            return new Result(200,state+"成功！");
        }else{
            return new Result(400,state+"失败！");
        }
    }

    @RequestMapping("/getDataStatistics")
    public List<Integer> getDataStatistics(){
        return articleService.getDataStatistics();
    }

    @RequestMapping("/getCategories")
    public List<String> getCategories(){
        return articleService.getCategories();
    }
}
